<template>
  <div class="storage-container">
    <el-card class="storage-card">
      <div slot="header" class="card-header">
        <span>库存操作</span>
        <div class="header-operations">
          <el-select v-model="queryParams.status" placeholder="请选择操作类型" clearable>
            <el-option label="入库" value="inStock"></el-option>
            <el-option label="出库" value="outStock"></el-option>
          </el-select>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
        </div>
      </div>
      
      <el-table
        :data="adjustmentList"
        style="width: 100%"
        v-loading="loading"
      >
        <el-table-column prop="productCode" label="商品编号" width="200" align="center"></el-table-column>
        <el-table-column prop="productName" label="商品名称" min-width="200" align="center"></el-table-column>
        <el-table-column prop="changeStock" label="数量" width="150" align="center">
          <template slot-scope="scope">
            <span :style="{ color: scope.row.changeStock > 0 ? '#67c23a' : '#f56c6c' }">
              {{ scope.row.changeStock > 0 ? '+' : '' }}{{ scope.row.changeStock }}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="reason" label="操作类型" width="180" align="center">
          <template slot-scope="scope">
            <el-tag :type="scope.row.reason === 'inStock' ? 'success' : 'danger'">
              {{ scope.row.reason === 'inStock' ? '入库' : '出库' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="operatorName" label="操作员" width="150" align="center"></el-table-column>
        <el-table-column prop="adjustmentTime" label="操作时间" width="250" align="center">
          <template slot-scope="scope">
            {{ formatDate(scope.row.adjustmentTime) }}
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryParams.page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="queryParams.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'StorageView',
  data() {
    return {
      queryParams: {
        page: 1,
        pageSize: 10,
        keyword: '',
        status: '',
        sortField: 'adjustmentTime',
        sortOrder: 'desc'
      },
      loading: false,
      total: 0,
      adjustmentList: []
    }
  },
  methods: {
    formatDate(dateString) {
      if (!dateString) return '';
      const date = new Date(dateString);
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, '0');
      const day = String(date.getDate()).padStart(2, '0');
      const hours = String(date.getHours()).padStart(2, '0');
      const minutes = String(date.getMinutes()).padStart(2, '0');
      const seconds = String(date.getSeconds()).padStart(2, '0');
      
      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    },
    handleSearch() {
      this.queryParams.page = 1;
      this.fetchData();
    },
    handleSizeChange(val) {
      this.queryParams.pageSize = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.queryParams.page = val;
      this.fetchData();
    },
    fetchData() {
      this.loading = true;
      
      this.$http.post('/inventory/adjustment/list', this.queryParams)
        .then(response => {
          if (response.code === 200 && response.data) {
            this.adjustmentList = response.data.records || [];
            this.total = response.data.total || 0;
          } else {
            // 错误会由全局拦截器处理
            this.adjustmentList = [];
            this.total = 0;
          }
        })
        .catch(error => {
          console.error('获取数据失败:', error);
          // 错误会由全局拦截器处理
          this.adjustmentList = [];
          this.total = 0;
        })
        .finally(() => {
          this.loading = false;
        });
    }
  },
  mounted() {
    this.fetchData();
  }
}
</script>

<style scoped>
.storage-container {
  padding: 20px;
  background: #f0f2f5;
  min-height: calc(100vh - 120px);
}

.storage-card {
  border-radius: 8px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #304156;
}

.header-operations {
  display: flex;
  gap: 10px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

/* Element UI 主题覆盖 */
:deep(.el-button--primary) {
  background-color: #ff6b00;
  border-color: #ff6b00;
}

:deep(.el-button--primary:hover),
:deep(.el-button--primary:focus) {
  background-color: #ff8533;
  border-color: #ff8533;
}

:deep(.el-select .el-input__inner:focus) {
  border-color: #ff6b00;
}

:deep(.el-select-dropdown__item.selected) {
  color: #ff6b00;
}

:deep(.el-pagination.is-background .el-pager li:not(.disabled).active) {
  background-color: #ff6b00;
}

:deep(.el-pagination.is-background .el-pager li:not(.disabled):hover) {
  color: #ff6b00;
}

.el-card {
  border-radius: 8px;
}

.el-card__header {
  padding: 15px 20px;
  border-bottom: 1px solid #e6e6e6;
}

.el-table {
  color: #304156;
}

.el-table th {
  background-color: #f5f7fa;
  color: #304156;
}

:deep(.el-tag--success) {
  background-color: rgba(103, 194, 58, 0.1);
  border-color: rgba(103, 194, 58, 0.2);
  color: #67c23a;
}

:deep(.el-tag--danger) {
  background-color: rgba(245, 108, 108, 0.1);
  border-color: rgba(245, 108, 108, 0.2);
  color: #f56c6c;
}
</style> 